package com.wuyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyb.mapper.AdminLogMapper;
import com.wuyb.pojo.AdminLog;
import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.entity.ResultUtils;
import com.wuyb.service.AdminLogService;
import com.wuyb.util.MyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * AdminLog服务实现类
 *
 * @Author: wyb
 * @Date: 2024/01/02/12:12
 * @Description:
 */

@Service
@Slf4j
public class AdminLogServiceImpl extends ServiceImpl<AdminLogMapper, AdminLog> implements AdminLogService {

    @Resource
    private AdminLogMapper adminLogMapper;

    /**
     * 新增用户操作日志记录
     *
     * @param actionModule
     * @param actionType
     * @param actionUrl
     * @param actionContent
     * @param actionSuccess
     */
    @Override
    public void insert(String actionModule, String actionType, String actionUrl, String actionContent, Boolean actionSuccess) {
        AdminLog adminLog = AdminLog.builder()
                .actionTime(LocalDateTime.now())
                .actionUserId(MyUtil.getUser().getId())
                .actionContent(actionContent)
                .actionType(actionType)
                .actionUrl(actionUrl)
                .actionModule(actionModule)
                .actionSuccess(actionSuccess)
                .build();

        adminLogMapper.insert(adminLog);
    }

    @Override
    public BaseResponse<Page<AdminLog>> select(Integer current, Integer size, Map<String, String> params) {
        QueryWrapper<AdminLog> wrapper = new QueryWrapper<>();
        String searchType = params.get("searchType");
        String key = params.get("key");

        if ("actionUserName".equals(searchType)) {
            wrapper.like("u.name_xq", key);
        } else if ("actionModule".equals(searchType)) {
            wrapper.eq("action_module_xq", key);
        } else if ("actionType".equals(searchType)) {
            wrapper.like("action_type_xq",key);
        } else if ("actionUrl".equals(searchType)) {
            wrapper.like("action_url_xq",key);
        } else if ("actionSuccess".equals(searchType)) {
            wrapper.eq("action_success_xq", key);
        } else if ("actionTime".equals(searchType)) {
            wrapper.ge("action_time_xq", params.get("startDate"))
                    .le("action_time_xq",params.get("endDate"));
        }
        return ResultUtils.success((Page<AdminLog>) adminLogMapper.selectWithUser(new Page<>(current,size), wrapper));
    }

    /**
     * 新增用户操作日志
     * @param actionModule
     * @param actionType
     * @param actionUrl
     * @param actionSuccess
     */
    @Override
    public void insert(String actionModule, String actionType, String actionUrl, Boolean actionSuccess) {
        insert(actionModule, actionType, actionUrl, "", actionSuccess);
    }
}
